﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using myTwitter.Model;
using System.Reflection;
using System.Windows.Media.Animation;
using myTwitter.BO;
using myBoard.Framework.Security;
using myBoard.Framework.Controls.Navigation;

namespace myTwitter
{
	/// <summary>
	/// Interaction logic for Main.xaml
	/// </summary>
	public partial class Main : IMagnet, IRequiresStorage
	{
        private Storyboard _switchToConnectedUserModeSB;
        private Storyboard _switchToLoginModeSB;
        private Storage _storage;
        private string _settingsPath;

        public Main()
		{
			this.InitializeComponent();
            GlobalController.Views.Main = this;
            _loginCtrl.SuccessfullyLoggedIn += new LoginScreen.SuccessfullyLoggedInHandler(_loginCtrl_SuccessfullyLoggedIn);
            _loggedInUserInfo.ShowUserDetailsRequested += new LoggedInUserInfo.ShowUserDetailsRequestedHandler(_loggedInUserInfo_ShowUserDetailsRequested);
            _switchToConnectedUserModeSB = this.Resources["SwitchToConnectedUserMode"] as Storyboard;
            _switchToLoginModeSB = this.Resources["SwitchToLoginMode"] as Storyboard;
		}

        void _loggedInUserInfo_ShowUserDetailsRequested(User user)
        {
            Grid g = ((TabItem)navigator._tabControl.SelectedItem).Content as Grid;
            BreadcrumbExplorer breadcrumbExplorer = g.Children[0] as BreadcrumbExplorer;
            breadcrumbExplorer.AddEntity(user);
        }

        void _loginCtrl_SuccessfullyLoggedIn(myTwitter.BO.User loggedInUser)
        {
            SwitchToConnectedUserMode();
        }

        public void SwitchToConnectedUserMode()
        {
            this.BeginStoryboard(_switchToConnectedUserModeSB);
            SaveState();
        }

        public void SwitchToLoginMode()
        {
            this.BeginStoryboard(_switchToLoginModeSB);
            _loginCtrl._passwordTbx.Password = string.Empty;
            _loginCtrl._loginTbx.Text = string.Empty;
            Keyboard.Focus(_loginCtrl._loginTbx);
        }

        public void HideMyBoard()
        {
            if (CloseMyBoardRequested != null)
                CloseMyBoardRequested(this, null);
        }

        #region IMagnet Members

        public string StoreID
        {
            get
            { return "{1A54E81E-2CB4-45b9-B0BC-E4EC7194CB4E}"; }
        }

        public event EventHandler CloseMyBoardRequested;

        public event OnFlipRequested FlipRequested;

        public Panel GetBackSide()
        {
            return _back;
        }

        public Panel GetFrontSide()
        {
            return _front;
        }

        public BitmapImage Icon
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "myTwitter.twitter2.png");
            }            
        }

        public BitmapImage Thumbnail
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "myTwitter.Images.thumbnail.png");
            }
        }

        public string MagnetName
        {
            get { return "Sinapse"; }
        }

        public void Terminate()
        {
            GlobalController.Instances.ITwitterProvider.StopAutoRefresh();
            GlobalController.Instances.ITwitterProvider.EndSession();
        }

        #endregion

        #region IRequiresStorage Members

        public void GetStorage(Storage storage)
        {
            _storage = storage;
            _settingsPath = System.IO.Path.Combine(_storage.StorageDirectory.FullName, "Settings.xml");
            GlobalController.Settings.MagnetSettings = Serializer.Load<MagnetSettings>("Sinapse", _settingsPath);
            if (GlobalController.Settings.MagnetSettings.UsersOAuthenSettings != null && GlobalController.Settings.MagnetSettings.UsersOAuthenSettings.Count > 0)
            {
                _loginCtrl._loginTbx.Text = GlobalController.Settings.MagnetSettings.UsersOAuthenSettings[0].Login;
                _loginCtrl._passwordTbx.Password = Encryption.Decrypt(GlobalController.Settings.MagnetSettings.UsersOAuthenSettings[0].Password);
            }
        }

        public void SaveState()
        {
            Serializer.Save<MagnetSettings>("Sinapse", GlobalController.Settings.MagnetSettings, _settingsPath);
        }

        #endregion
    }
}